//
//  HWCashView.m
//  嗨文
//
//  Created by 响当当 on 2019/2/18.
//  Copyright © 2019年 dongzheng. All rights reserved.
//

#import "HWReadRecordView.h"
#import "HWMyReadRecordCell.h"

@interface HWReadRecordView()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic, strong)UITableView *tableView;
@property (nonatomic, strong)NSMutableArray *bookArr;
@property (nonatomic, strong)NSString *page;
@property (nonatomic, assign)BOOL hasNextPage;

@end

@implementation HWReadRecordView

- (NSMutableArray *)bookArr
{
    if (!_bookArr)
    {
//        _bookArr = @[@{@"bookTitle":@"修真界唯一锦鲤 连载中",@"bookAuthor":@"萧瑾瑜",@"bookUpdateTime":@"5小时前"},
//                     @{@"bookTitle":@"修真界唯一锦鲤 已完结",@"bookAuthor":@"萧瑾瑜",@"bookUpdateTime":@"5小时前"},
//                     @{@"bookTitle":@"修真界唯一锦鲤 连载中",@"bookAuthor":@"萧瑾瑜",@"bookUpdateTime":@"5小时前"}];
        _bookArr = [[NSMutableArray alloc]init];
    }
    return _bookArr;
}

- (UITableView *)tableView
{
    if (!_tableView)
    {
        _tableView = [[UITableView alloc]initWithFrame:CGRectMake(0,0,SCREEN_WIDTH,SCREEN_HEIGHT-60*kUIScaleBasePlus) style:UITableViewStylePlain];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.separatorStyle = 0;
        _tableView.backgroundColor = [UIColor groupTableViewBackgroundColor];
        
        //上拉刷新
        MJRefreshAutoNormalFooter *footer = [MJRefreshAutoNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(loadNewData)];
        //设置上拉刷新属性
        [footer setTitle:@"加载更多" forState:MJRefreshStateIdle];
        [footer setTitle:@"Loading ..." forState:MJRefreshStateRefreshing];
        
        // 设置字体
        footer.stateLabel.font = [UIFont systemFontOfSize:14];
        
        // 设置颜色
        footer.stateLabel.textColor = LCHexColor(0x1f1f1f);
        
        _tableView.mj_footer = footer;

    }
    return _tableView;
}

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self)
    {
        self.page = @"1";
        self.hasNextPage = YES;
        [self addSubview:self.tableView];
    }
    return self;
}

#pragma mark - tableView delegate
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.bookArr.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 88*kUIScaleBasePlus;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    HWMyReadRecordCell *cell = [tableView dequeueReusableCellWithIdentifier:@"HWReadRecordCell"];
    
    if (!cell)
    {
        cell = [[HWMyReadRecordCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"HWReadRecordCell"];
        cell.selectionStyle = 0;
    }
    
    if ([self.bookArr isValidArray])
    {
        HWReadRecordModel *model = [[HWReadRecordModel alloc]initWithDic:self.bookArr[indexPath.row]];
        
        [cell getBookDetail:model andTag:indexPath.row];
        
        WeakSelf(self)
        cell.getBackAddNumBlock = ^(NSString *num) {
            [weakSelf addBook:num];
        };
    }
    
    return cell;
}


- (void)getReadRecordList:(NSArray *)arr isNextPage:(NSNumber *)hasNext
{
    [SVProgressHUD dismiss];
    if ([arr isValidArray])
    {
        if (self.hasNextPage)
        {
            [self.bookArr addObjectsFromArray:arr];
            self.hasNextPage = [hasNext boolValue];
            [self.tableView reloadData];
        }
        else
        {
            self.tableView.mj_footer.hidden = YES;
        }
        
    }
}

- (void)addBook:(NSString *)tag
{
    HWReadRecordModel *model = [[HWReadRecordModel alloc]initWithDic:self.bookArr[tag.integerValue]];

    NSDictionary *prarmers = @{
                               @"access_token" : current_User.token,
                               @"bookId" : [NSString stringWithFormat:@"%@",model.bookId]
                               };
    
    [NetWorkingManager postRequestUrl:FULL_WEB_URL(KAPI_BOOKREADLIKE) withPramater:prarmers sucess:^(id responseObject) {
        
    } failure:^(NSError *error) {
        
    }];
}


- (void)loadNewData
{
    [SVProgressHUD showWithStatus:@"加载中"];

    WeakSelf(self)
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        
        if (weakSelf.getBackInfoBlock)
        {
            if (self.hasNextPage)
            {
                NSInteger num = self.page.integerValue + 1;
                self.getBackInfoBlock([NSString stringWithFormat:@"%ld",num]);
                self.page = [NSString stringWithFormat:@"%ld",num];
            }
            else
            {
                self.getBackInfoBlock(@"1");
            }
        }
        
        [weakSelf.tableView.mj_footer endRefreshing];
    });
}

@end
